package com.berchen.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.DefaultManagedAwareThreadFactory;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 自定义线程池
 */
@Configuration
public class ThreadPoolConfig {

    @Value("${corePoolSize}")
    private int corePoolSize;
    @Value("${maxNumPoolSize}")
    private int maxNumPoolSize;
    @Value("${keepAliveTime}")
    private long keepAliveTime;

    /**
     * 创建自定义线程池
     * @return
     */
    @Bean(name = "threadPoolExector")
    public ThreadPoolExecutor threadPoolExecutor(){

        return new ThreadPoolExecutor(
                corePoolSize,
                maxNumPoolSize,
                keepAliveTime,
                TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<>(40),
                new DefaultManagedAwareThreadFactory(),
                new ThreadPoolExecutor.CallerRunsPolicy());
    }
}
